package com.bugstack.domain.credit.service;

import com.bugstack.domain.award.model.valobj.TaskStateVO;
import com.bugstack.domain.credit.event.CreditAdjustSuccessEvent;
import com.bugstack.domain.credit.model.aggregate.TradeAggregate;
import com.bugstack.domain.credit.model.entity.CreditAccountEntity;
import com.bugstack.domain.credit.model.entity.CreditOrderEntity;
import com.bugstack.domain.credit.model.entity.TaskEntity;
import com.bugstack.domain.credit.model.entity.TradeEntity;
import com.bugstack.domain.credit.model.valobj.TradeNameVO;
import com.bugstack.domain.credit.model.valobj.TradeTypeVO;
import com.bugstack.domain.credit.repository.ICreditRepository;
import com.bugstack.types.event.BaseEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @Author: czb
 * @Description: TODO
 * @DateTime: 2025/4/15 14:32
 **/
@Slf4j
@Service
public class CreditAdjustService implements ICreditAdjustService {

    @Resource
    private ICreditRepository repository;
    @Resource
    private CreditAdjustSuccessEvent creditAdjustSuccessEvent;

    @Override
    public String createOrder(TradeEntity tradeEntity) {
        String userId = tradeEntity.getUserId();
        TradeNameVO tradeName = tradeEntity.getTradeName();
        TradeTypeVO tradeType = tradeEntity.getTradeType();
        BigDecimal amount = tradeEntity.getAmount();
        String outBusinessNo = tradeEntity.getOutBusinessNo();
        log.info("创建积分订单开始 userId: {} tradeName:{} tradeType:{} amount:{}", userId, tradeName, tradeType, amount);

        //创建积分调整对象
        CreditAccountEntity creditAccountEntity = TradeAggregate.createCreditAccountEntity(userId, amount);

        //创建积分调整流水订单
        CreditOrderEntity creditOrderEntity = TradeAggregate.createCreditOrderEntity(userId, tradeName, tradeType, amount, outBusinessNo);

        //构建消息
        CreditAdjustSuccessEvent.CreditAdjustSuccessMessage creditAdjustSuccessMessage = new CreditAdjustSuccessEvent.CreditAdjustSuccessMessage();
        creditAdjustSuccessMessage.setUserId(creditAccountEntity.getUserId());
        creditAdjustSuccessMessage.setOrderId(creditOrderEntity.getOrderId());
        creditAdjustSuccessMessage.setAmount(amount);
        creditAdjustSuccessMessage.setOutBusinessNo(outBusinessNo);

        BaseEvent.EventMessage<CreditAdjustSuccessEvent.CreditAdjustSuccessMessage> creditAdjustSuccessMessageEvent= creditAdjustSuccessEvent.buildEventMessage(creditAdjustSuccessMessage);

        //构建任务
        TaskEntity taskEntity = new TaskEntity();
        taskEntity.setUserId(userId);
        taskEntity.setState(TaskStateVO.create);
        taskEntity.setTopic(creditAdjustSuccessEvent.topic());
        taskEntity.setMessageId(creditAdjustSuccessMessageEvent.getId());
        taskEntity.setMessage(creditAdjustSuccessMessageEvent);

        //构建事务聚合对象
        TradeAggregate tradeAggregate = new TradeAggregate();
        tradeAggregate.setUserId(userId);
        tradeAggregate.setCreditAccountEntity(creditAccountEntity);
        tradeAggregate.setCreditOrderEntity(creditOrderEntity);
        tradeAggregate.setTaskEntity(taskEntity);

        //持久化事务聚合对象
        repository.saveTradeAggregate(tradeAggregate);
        log.info("创建积分订单结束 userId: {} tradeName:{} tradeType:{} amount:{} orderId: {}", userId, tradeName, tradeType, amount, creditOrderEntity.getOrderId());

        return creditOrderEntity.getOrderId();
    }

    @Override
    public CreditAccountEntity queryUserCreditAccount(String userId) {
        return repository.queryUserCreditAccount(userId);
    }
}
